using System;
using Xunit;

namespace LanguageLearnTestCase.Time
{
    public class WeekRegionCalc
    {
        [Theory]
        [InlineData("2021-03-16 12:15:44", "2021-03-15 12:15:44")]
        [InlineData("2021-03-18 12:15:44", "2021-03-15 12:15:44")]
        [InlineData("2021-03-21 12:15:44", "2021-03-15 12:15:44")]
        public void TestGetWeekRegionStart(string timeInput, string timeOutput)
        {
            DateTime time = Convert.ToDateTime(timeInput);
            Assert.Equal(timeOutput, GetWeekRegionStart(time).ToString("yyyy-MM-dd HH:mm:ss"));
        }
        [Theory]
        [InlineData("2021-03-16 12:15:44", "2021-03-21 12:15:44")]
        [InlineData("2021-03-18 12:15:44", "2021-03-21 12:15:44")]
        [InlineData("2021-03-21 12:15:44", "2021-03-21 12:15:44")]
        public void TestGetWeekRegionEnd(string timeInput, string timeOutput)
        {
            DateTime time = Convert.ToDateTime(timeInput);
            Assert.Equal(timeOutput, GetWeekRegionEnd(time).ToString("yyyy-MM-dd HH:mm:ss"));
        }

        /// <summary>
        /// 获取周范围时间开始日期
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns>开始日期, 时分秒值随原数据不变</returns>
        public static DateTime GetWeekRegionStart(DateTime time)
        {
            int week = (int)time.DayOfWeek;
            week = week == 0 ? 7 : week;
            return time.AddDays(-week + 1);
        }
        /// <summary>
        /// 获取周范围时间结束日期
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns>结束日期, 时分秒值随原数据不变</returns>
        public static DateTime GetWeekRegionEnd(DateTime time)
        {
            int week = (int)time.DayOfWeek;
            week = week == 0 ? 7 : week;
            return time.AddDays(7 - week);
        }
    }
}
